/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FilterFrame.java
 *
 * Created on 03/04/2012, 16:47:08
 */
package datainsert.frames;

import datainsert.DBExceptionToken;
import datainsert.controlers.ListUtils;
import datainsert.controlers.ListenersControler;
import datainsert.dbinterface.DBDispositivo;
import datainsert.dbinterface.DBInterface;
import datainsert.dbinterface.DBNo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author user
 */
public class FilterFrame extends javax.swing.JFrame {

    private MainFrame frame;
    /** Creates new form FilterFrame */
    public FilterFrame(MainFrame frame) {
        this.frame = frame;
        DBInterface dbinterface = frame.getDBInterface();
        DBNo no = frame.getCurrentNo();
        DBDispositivo devobj;
        Connection conn = frame.getDBInterface().getConn();
        
        Date [] d = dbinterface.getDate(no.getTable(), new DBExceptionToken());
        String [] dev = dbinterface.devicesFromDataTable(no.getTable(), null, new DBExceptionToken());
        
        initComponents();
        
        jcmbDate1.setModel(new DefaultComboBoxModel(d));
        jcmbDate2.setModel(new DefaultComboBoxModel(d));
        
        for ( int i = 0; i < dev.length; i++) {
            devobj = no.getDevice(conn, dev[i]);
            ListUtils.addContent(jlstAdd, dev[i] + " " + devobj.getCod());
        }
        
        FilterActuator act = new FilterActuator();
        ListenersControler.addButtonActionListener(act, jbtAdd);
        ListenersControler.addButtonActionListener(act, jbtRm);
        ListenersControler.addButtonActionListener(act, jbtCancel);
        ListenersControler.addButtonActionListener(act, jbtRemove);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jcmbDate1 = new javax.swing.JComboBox();
        jcmbDate2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jtxtFilter1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jlstRm = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlstAdd = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jbtAdd = new javax.swing.JButton();
        jbtRm = new javax.swing.JButton();
        jbtRemove = new javax.swing.JButton();
        jbtCancel = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jtxtFilter2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("De:");

        jcmbDate1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmbDate1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Até:");

        jtxtFilter1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtFilter1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Valor do dado:");

        jScrollPane2.setViewportView(jlstRm);

        jScrollPane1.setViewportView(jlstAdd);

        jLabel4.setText("Dispositivos:");

        jLabel5.setText("A remover:");

        jbtAdd.setText(">>");

        jbtRm.setText("<<");

        jbtRemove.setText("Remover");

        jbtCancel.setText("Cancelar");

        jLabel6.setText("De:");

        jLabel7.setText("Até:");

        jtxtFilter2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtFilter2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jcmbDate1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 249, Short.MAX_VALUE)
                            .addComponent(jLabel3)
                            .addComponent(jcmbDate2, javax.swing.GroupLayout.Alignment.TRAILING, 0, 249, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel4)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jtxtFilter1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jbtRm, 0, 0, Short.MAX_VALUE)
                                            .addComponent(jbtAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(14, 14, 14)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jtxtFilter2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(jLabel2)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(80, 80, 80)
                        .addComponent(jbtRemove, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcmbDate1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcmbDate2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jtxtFilter1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(jtxtFilter2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jbtAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtRm)
                        .addGap(86, 86, 86)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtRemove)
                    .addComponent(jbtCancel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtxtFilter1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtFilter1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtFilter1ActionPerformed

    private void jcmbDate1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmbDate1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcmbDate1ActionPerformed

    private void jtxtFilter2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtFilter2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtFilter2ActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtAdd;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtRemove;
    private javax.swing.JButton jbtRm;
    private javax.swing.JComboBox jcmbDate1;
    private javax.swing.JComboBox jcmbDate2;
    private javax.swing.JList jlstAdd;
    private javax.swing.JList jlstRm;
    private javax.swing.JTextField jtxtFilter1;
    private javax.swing.JTextField jtxtFilter2;
    // End of variables declaration//GEN-END:variables

    
    class FilterActuator implements ActionListener {
        
        public void actionPerformed(ActionEvent e) {
            
            if ( e.getSource() == jbtRemove ) {
                if ( (jtxtFilter1.getText().trim().compareTo("") != 0
                     && jtxtFilter1.getText() != null)
                     ||   (jtxtFilter2.getText().trim().compareTo("") != 0
                     && jtxtFilter2.getText() != null)) {
                    
                    if (jlstRm.getModel().getSize() > 0) {
                        
                        int opt = JOptionPane.YES_OPTION;
                        String date1 = String.valueOf(jcmbDate1.getModel().getSelectedItem());
                        String date2 = String.valueOf(jcmbDate2.getModel().getSelectedItem());
                        String aux;
                        String table = frame.getCurrentNo().getTable();
                        DBExceptionToken expt = new DBExceptionToken();

                        ArrayList<String> s = new ArrayList<String>();
                        String str[];
                        s.add("data BETWEEN TO_DATE('" + date1 + "','YYYY-MM-DD') AND "
                                + "TO_DATE('" + date2 + "','YYYY-MM-DD')");
                        if (jtxtFilter1.getText().compareTo("*") != 0 && (jtxtFilter2.getText().trim().compareTo("") == 0
                             || jtxtFilter2.getText() == null))
                            s.add("dado = " + jtxtFilter1.getText());
                        else if (jtxtFilter2.getText().compareTo("*") != 0 && (jtxtFilter1.getText().trim().compareTo("") == 0
                             || jtxtFilter1.getText() == null))
                            s.add("dado = " + jtxtFilter2.getText());
                        else if (jtxtFilter2.getText().compareTo("*") != 0 && jtxtFilter1.getText().compareTo("*") != 0)
                            s.add("dado BETWEEN " + jtxtFilter1.getText() + " AND " + jtxtFilter2.getText());
                        else 
                            opt = JOptionPane.showConfirmDialog(null, 
                                    "Você deseja remover todos os dados referêntes às datas e dispositivos escolhidos?", 
                                    "Remoção", JOptionPane.YES_NO_OPTION);

                        if (opt == JOptionPane.YES_OPTION) {
                            str = new String[s.size()+1];
                            for ( int i = 0; i < jlstRm.getModel().getSize(); i++ ) {
                                aux = String.valueOf(jlstRm.getModel().getElementAt(i));
                                s.add("canal_aquisicao = " + aux.substring(0, aux.indexOf(" ")));
                                s.toArray(str);
                                for ( int j = 0; j < str.length; j++ )System.out.println(str[j]);
                                frame.getDBInterface().remove(table, str, expt);
                                s.remove(s.size()-1);
                            }
                        }
                    } else
                        JOptionPane.showMessageDialog(null, "Você deve selecionar pelo menos um dispositivo para a filtragem de dados.");
                }
                else 
                    JOptionPane.showMessageDialog(null, "Você deve digitar um valor para a filtragem de dados.");
            }
            
            if ( e.getSource() == jbtAdd )            
                ListUtils.list2List(jlstAdd, jlstRm);
            
            if( e.getSource() == jbtRm ) 
                ListUtils.list2List(jlstRm,jlstAdd);
            
            if ( e.getSource() == jbtCancel )            
                dispose();
            
       }
            
        
    }

}
